﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SDT.Model
{
    public class ProviderManager
    {
        public static void Add(Provider p)
        {
            try
            {
                SDTDataContext db = new SDTDataContext();
                if (p != null)
                {
                    db.Providers.InsertOnSubmit(p);
                    db.SubmitChanges();
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static void Delete(int Id)
        {
            try
            {
                SDTDataContext db = new SDTDataContext();
                db.Providers.DeleteAllOnSubmit(from d in db.Providers where d.ID == Id select d);
                db.SubmitChanges();

            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static bool CheckExist(int Id)
        {
            SDTDataContext db = new SDTDataContext();
            var l = (from a in db.Providers
                     where a.ID == Id
                     select a).ToList();

            return (l != null && l.Count > 0) ? true : false;
        }


        public static void Update(Provider p)
        {
            try
            {
                SDTDataContext db = new SDTDataContext();
                if (p != null)
                {
                    var obj = (from cd in db.Providers
                               where cd.ID == p.ID
                               select cd).Single();
                    obj.Address = p.Address;
                    obj.Email = p.Email;
                    obj.ModBy = UserManager.GetUserId();
                    obj.ModDate = DateTime.Now;
                    obj.Fax = p.Fax;
                    obj.ID = p.ID;
                    obj.Introduction = p.Introduction;
                    obj.LogoPath = p.LogoPath;
                    obj.Name = p.Name;
                    obj.Phone = p.Phone;
                    
                    db.SubmitChanges();
                }
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
        public static Provider GetOne(int id)
        {
            try
            {
                SDTDataContext db = new SDTDataContext();
                var l = (from c in db.Providers
                         where c.ID == id
                         select c).ToList();
                if (l != null && l.Count > 0)
                {
                    return l.First();
                }
                else return null;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }

        public static IList<Provider> SelectList(string keyword)
        {
            try
            {
                SDTDataContext db = new SDTDataContext();
                var l = (from c in db.Providers
                         where (keyword!=string.Empty? c.Name.Contains(keyword):(1==1))
                         select c).ToList();
                return l;
            }
            catch (Exception exp)
            {
                throw exp;
            }
        }
    }
}
